package com.mycompany.service;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.LockModeType;
import javax.persistence.PersistenceContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;

import com.mycompany.dao.UserDao;
import com.mycompany.model.User;
import com.mycompany.repositories.UserRepository;


@Service
public class UserService {
	
	@Autowired
	UserRepository userRepository;
	
	@PersistenceContext
	EntityManager entityManager;
	
	public void saveUser(User user){
		userRepository.save(user);
		
	}
	
	public List<User> selectAll(){
		
			if(entityManager == null)
				System.out.println("Entity Manager null");
			else
				System.out.println("Entity Manager not null "+entityManager);
		return userRepository.findAll();
	}
	
	public User findByEmail(String email){
		return userRepository.findByEmail(email);
	}
	
	public void deleteUser(User user){
	
		userRepository.delete(user);
		
	}
	
	public List<User> findByCognome(int startIndex, int numId)
	{
		
		int page = startIndex / 5;
		
		Pageable pageable = new PageRequest(page,numId);
		Page<User> result = userRepository.findAll(pageable);
		List<User> list = result.getContent();
		return list;
	}
	
	public User findOne(Long idUser){
		
		User user = entityManager.find(User.class,idUser);
		return user;
		
	}
	
	
//	public List<User> pageableUsers(int startIndex, int endIndex){
//		return userRepository(startIndex, endIndex);
//	}
//	
//	


}
